package cn.ubeta.b2c.widget;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.ubeta.b2c.R;
import cn.ubeta.b2c.cart.CartActivity;

/**
 * Created by denny on 16/8/17.
 */

public class TitleBar extends RelativeLayout {

    private TextView mTvTitle;
    private ImageView mIvTitle;
    private TextView mTvAction;
    private TextView mTvCartNum;
    private View mIvCart;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.widget_title_bar, this);
        init(context, attrs);
    }

    /**
     * 初始化控件
     * @param attrs
     */
    private void init(Context context,AttributeSet attrs) {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mIvTitle = (ImageView) findViewById(R.id.iv_title);
        mTvAction = (TextView) findViewById(R.id.tv_right_action);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        boolean isTitleCenter = true;// TODO 标题是否居中 a.getBoolean(R.styleable.TitleBar_is_center_title, false);
        boolean showLeftIcon = a.getBoolean(R.styleable.TitleBar_show_left_icon, true);
        String title = a.getString(R.styleable.TitleBar_tb_title);
        // 返回按钮图标
        Drawable backIcon = a.getDrawable(R.styleable.TitleBar_tb_back_icon);

        ImageView ivLeft = (ImageView) findViewById(R.id.iv_left);
        if (!showLeftIcon) {
            ivLeft.setVisibility(GONE);
        } else if (backIcon != null) {
            ivLeft.setImageDrawable(backIcon);
        }

        // 是否标题居中
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)(mTvTitle.getLayoutParams());
        if (isTitleCenter) {
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            mTvTitle.setGravity(Gravity.CENTER);
        } else {
            //params.addRule(RelativeLayout.RIGHT_OF, R.id.tv_left);
            //params.addRule(RelativeLayout.LEFT_OF, R.id.tv_right);
        }

        if (!TextUtils.isEmpty(title)) {
            mTvTitle.setText(title);
        }

        // 是否底部分割线
        boolean showDivider = a.getBoolean(R.styleable.TitleBar_tb_show_divider, true);
        findViewById(R.id.view_divider).setVisibility(showDivider ? VISIBLE : GONE);
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title) {
        mIvTitle.setVisibility(GONE);
        mTvTitle.setText(title);
        mTvTitle.setVisibility(VISIBLE);
    }

    /**
     * 设置标题图标
     * @param resIcon
     */
    public void setIcon(int resIcon) {
        mTvTitle.setVisibility(GONE);
        mIvTitle.setImageResource(resIcon);
        mIvTitle.setVisibility(VISIBLE);
    }

    /**
     * 设置右侧动作
     * @param actionName
     */
    public void setAction(String actionName, OnClickListener listener) {
        if (!TextUtils.isEmpty(actionName)) {
            mTvAction.setText(actionName);
            mTvAction.setOnClickListener(listener);
            mTvAction.setVisibility(VISIBLE);
        } else if (mTvAction.getVisibility() == VISIBLE) {
            mTvAction.setVisibility(GONE);
        }
    }

    /**
     * 设置左侧图标的监听
     * @param leftClickListener
     */
    public void setLeftClickListener(OnClickListener leftClickListener) {
        findViewById(R.id.iv_left).setOnClickListener(leftClickListener);
    }

    /**
     * 刷新购物车个数
     * @param count
     * @param showAnimation
     */
    public void refreshCartCount(int count, boolean showAnimation) {
        if (mTvCartNum == null) {
            View view = ((ViewStub) findViewById(R.id.vs_titlebar_cart)).inflate();
            mTvCartNum = (TextView) view.findViewById(R.id.tv_cart_count);
            view.setVisibility(VISIBLE);
            // 跳转购物车
            view.setOnClickListener(v -> CartActivity.gotoActivity(getContext(), CartActivity.class));
        }

        if (count > 0) {
            mTvCartNum.setText(String.valueOf(count));
            if (mTvCartNum.getVisibility() != VISIBLE)
                mTvCartNum.setVisibility(VISIBLE);
        } else {
            mTvCartNum.setVisibility(View.GONE);
        }

        // 购物车动画
        if (showAnimation) {
            if (mIvCart == null)
                mIvCart = findViewById(R.id.iv_cart);
            startCartAnimation(mIvCart, mTvCartNum);
        }
    }

    /**
     * 购物车动画
     * @param cartView
     * @param countView
     */
    private void startCartAnimation(View cartView, View countView) {
        // 缩放数量
        if (countView.getVisibility() == VISIBLE) {
            float scaleSmall = 1.25f;
            float scaleLarge = 1.5f;
            PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                    Keyframe.ofFloat(0f, 1.0f),
                    Keyframe.ofFloat(0.25f, scaleSmall),
                    Keyframe.ofFloat(0.5f, scaleLarge),
                    Keyframe.ofFloat(0.75f, scaleSmall),
                    Keyframe.ofFloat(1.0f, 1.0f)
            );
            PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                    Keyframe.ofFloat(0f, 1.0f),
                    Keyframe.ofFloat(0.25f, scaleSmall),
                    Keyframe.ofFloat(0.5f, scaleLarge),
                    Keyframe.ofFloat(0.75f, scaleSmall),
                    Keyframe.ofFloat(1.0f, 1.0f)
            );

            ObjectAnimator scaleObjectAnimator = ObjectAnimator.ofPropertyValuesHolder(countView, scaleXValuesHolder, scaleYValuesHolder);
            scaleObjectAnimator.setDuration(500);
            scaleObjectAnimator.setRepeatCount(1);
            scaleObjectAnimator.start();
        }

        // 摇动购物车:先往左再往右
        if (cartView.getVisibility() == VISIBLE) {
            float shakeDegrees = 45f;
            PropertyValuesHolder rotateValuesHolder = PropertyValuesHolder.ofKeyframe(View.ROTATION,
                    Keyframe.ofFloat(0f, 0f),
                    Keyframe.ofFloat(0.2f, shakeDegrees),
                    Keyframe.ofFloat(0.4f, -shakeDegrees),
                    Keyframe.ofFloat(0.6f, shakeDegrees),
                    Keyframe.ofFloat(0.8f, -shakeDegrees),
                    Keyframe.ofFloat(1.0f, 0f)
            );
            ObjectAnimator rotateObjectAnimator = ObjectAnimator.ofPropertyValuesHolder(cartView, rotateValuesHolder);
            rotateObjectAnimator.setDuration(1000);
            rotateObjectAnimator.start();
        }
    }
}
